package org.vectrics.recruiter.struts;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;

import org.apache.log4j.Category;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vectrics.SystemException;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.JobAgent;
import org.vectrics.recruiter.user.JobAgentFunction;
import org.vectrics.recruiter.user.JobAgentLocation;

public class ShowJobAgentAction extends Action {
    private static Category log = Category.getInstance(ShowJobAgentAction.class);

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws JspException {
        log.info("execute() called");
        try {
            String jobAgentId = request.getParameter("jobAgentId");

            JobAgentForm jobAgentForm = null;
            if (form == null) {
                jobAgentForm = new JobAgentForm();
                form = jobAgentForm;
            } else {
                jobAgentForm = (JobAgentForm) form;
            }

            request.setAttribute(mapping.getAttribute(), jobAgentForm);

            jobAgentForm.setPersonId(request.getParameter("personId"));
            jobAgentForm.setJobAgentId(jobAgentId);
            JobAgent agent = null;
            if (jobAgentId != null) {

                agent = RecruitServiceLocator.getRecruitUserService().findJobAgent(new Long(jobAgentId));

                jobAgentForm.setDaysBack(agent.getDaysBack().toString());
                jobAgentForm.setJobAgentId(agent.getId().toString());
                jobAgentForm.setName(agent.getName());
                jobAgentForm.setSearchPhrase(agent.getSearchPhrase());
                if (agent != null) {
                    Iterator locIter = agent.getLocations().iterator();
                    if (locIter.hasNext()) {
                        JobAgentLocation location = (JobAgentLocation) locIter.next();
                        jobAgentForm.setLocationId1(location.getLocation().getId().toString());
                    }
                    if (locIter.hasNext()) {
                        JobAgentLocation location = (JobAgentLocation) locIter.next();
                        jobAgentForm.setLocationId2(location.getLocation().getId().toString());
                    }
                    if (locIter.hasNext()) {
                        JobAgentLocation location = (JobAgentLocation) locIter.next();
                        jobAgentForm.setLocationId3(location.getLocation().getId().toString());
                    }

                    Iterator functionIter = agent.getJobFunctions().iterator();
                    if (functionIter.hasNext()) {
                        JobAgentFunction jobFunction = (JobAgentFunction) functionIter.next();
                        jobAgentForm.setFunctionCode1(jobFunction.getJobFunction().getCode());
                    }
                    if (functionIter.hasNext()) {
                        JobAgentFunction jobFunction = (JobAgentFunction) functionIter.next();
                        jobAgentForm.setFunctionCode2(jobFunction.getJobFunction().getCode());
                    }
                    if (functionIter.hasNext()) {
                        JobAgentFunction jobFunction = (JobAgentFunction) functionIter.next();
                        jobAgentForm.setFunctionCode3(jobFunction.getJobFunction().getCode());
                    }
                }
            }
        } catch (Exception ex) {
            log.info("Exception in action", ex);
            throw new SystemException("Exception in action", ex);
        }

        log.info("execute() - finished.");

        return (mapping.findForward("success"));
    }
}
